package com.example.shop_2305.car

import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.shop_2305.R
import com.example.shop_2305.adapter.CartAdapter
import com.example.shop_2305.databinding.ActivityCarBinding
import com.example.shop_2305.fg.CartViewModel

@Route(path = "/app/CarActivity")
class CarActivity : BaseActivity<ActivityCarBinding,CartViewModel>() {
    lateinit var cartAdapter: CartAdapter
    override fun initView() {
        cartAdapter = CartAdapter()
        binding.rv.adapter = cartAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        viewModel.cart("true","10","1")

        //左滑删除
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val i = viewHolder.layoutPosition
                //先调接口
               /* var ids = arrayOf<Int>()
                ids+=cartAdapter.data[i].id*/   //多条记录使用
                viewModel.cartDelete(arrayOf(cartAdapter.data[i].id))
                //删除页面数据
                cartAdapter.data.removeAt(i)
                cartAdapter.notifyItemRemoved(i)//局部刷新
                checkBottom()//修改底部的全选状态和金额

            }
        }).attachToRecyclerView(binding.rv)

        //点击选中 + -
        cartAdapter.addChildClickViewIds(R.id.cart_cb,R.id.cart_add,R.id.cart_sub)
        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.cart_cb->{
                    cartAdapter.data[position].isChecked = !cartAdapter.data[position].isChecked
                }
                R.id.cart_add->{
                    cartAdapter.data[position].cartNum++
                    (cartAdapter.getViewByPosition(position,R.id.cart_num) as TextView).text = cartAdapter.data[position].cartNum.toString()
                    //调接口
                    viewModel.cartUpdate(cartAdapter.data[position].id,cartAdapter.data[position].cartNum)
                    (cartAdapter.getViewByPosition(position,R.id.cart_sub) as TextView).isEnabled = true
                }
                R.id.cart_sub->{
                    if(cartAdapter.data[position].cartNum==1){
                        ToastUtils.showLong("不能再减少了")
                    }else{
                        cartAdapter.data[position].cartNum--
                        (cartAdapter.getViewByPosition(position,R.id.cart_num) as TextView).text = cartAdapter.data[position].cartNum.toString()
                        //调接口
                        viewModel.cartUpdate(cartAdapter.data[position].id,cartAdapter.data[position].cartNum)
                        if(cartAdapter.data[position].cartNum==1){
                            (cartAdapter.getViewByPosition(position,R.id.cart_sub) as TextView).isEnabled = false
                        }

                    }
                }
            }
            checkBottom()
        }

        //点击全选按钮，实现选中效果
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cb.setOnClickListener {
                cartAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                cartAdapter.notifyDataSetChanged()
                checkBottom()
            }

        }

    }

    private fun checkBottom() {
        //怎么判断是否全选了  当选中数量不为0 且和列表数量相同时为选中状态
        val size = cartAdapter.data.filter { it.isChecked }.size
        if(size!=0&&size==cartAdapter.data.size){
            binding.cb.isChecked = true
        }else{
            binding.cb.isChecked = false
        }
        binding.cb.text = "全选($size)"
        var amount = 0f
        cartAdapter.data.filter { it.isChecked }.forEach {
            amount+=it.cartNum*it.price.toFloat()
        }
        binding.money.text = "合计:￥$amount"
    }

    override fun initData() {
        viewModel.cartSuccess.observe(this){
            if(it.code==200){
                cartAdapter.data.clear()
                cartAdapter.data.addAll(it.data.list)
                cartAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CartViewModel>  = CartViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_car

}